package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.annotation.RepeatSubmit;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.domain.account.CommonWithdrawConfig;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallStoreSetmeal;
import com.tencreat.domain.mall.dto.MallGoodsAddDTO;
import com.tencreat.domain.mall.dto.MallStoreGoodsDTO;
import com.tencreat.domain.mall.vo.MallStoreGoodsVO;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallStoreSetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述信息
 * 套餐分类 Controller
 *
 * @Author yaoruiqi
 * @Company: 深圳腾创互联科技有限公司
 * @link: https://tencreat.com
 * @Date: 2023/11/10 18:56
 */

@RestController
@RequestMapping("/mall/storeSetmeal")
@Api(tags = "店铺商品-套餐分类")
public class MallStoreSetmealController extends BaseController {

    @Autowired
    private IMallStoreSetmealService mallStoreSetmealService;
    @Autowired
    private IMallGoodsService mallGoodsService;

    @ApiOperation("新增套餐分类")
    @PostMapping("/add")
//    @PreAuthorize("@ss.hasPermi('mall:store-goods:add')")
    @RepeatSubmit
    @Log(title = "商品管理>店铺-套餐分类", businessType = BusinessType.INSERT)
    HttpResult add(@RequestBody @Validated MallStoreSetmeal mallStoreSetmeal) {

        mallStoreSetmeal.setUserId(getLoginCheck().getUser().getUserId());
        return HttpResult.ok(mallStoreSetmealService.add(mallStoreSetmeal));
    }


    @ApiOperation(value = "店铺查询套餐分类列表")
    @PostMapping("/list")
    HttpResult<List<MallStoreSetmeal>> list(@RequestBody MallStoreSetmeal mallStoreSetmeal) {
        mallStoreSetmeal.setUserId(getLoginCheck().getUser().getUserId());
        QueryWrapper<MallStoreSetmeal> wrapper = Wrappers.query(mallStoreSetmeal);
        return HttpResult.ok(mallStoreSetmealService.selectList(wrapper));
    }

    @ApiOperation(value = "删除套餐分类")
    @Log(title = "套餐分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    HttpResult<Boolean> remove(@RequestBody MallStoreSetmeal mallStoreSetmeal) {
        //查到套餐分类下是否有商品
//        mallGoodsService
        MallStoreSetmeal one = mallStoreSetmealService.lambdaQuery().eq(MallStoreSetmeal::getId, mallStoreSetmeal.getId()).one();
        if (one == null) {
            return HttpResult.error("套餐分类有误！");
        } else {
            List<MallGoods> list = mallGoodsService.lambdaQuery().eq(MallGoods::getSetmealId, mallStoreSetmeal.getId()).list();
            if (list != null && list.size() > 0) {
                return HttpResult.error("该套餐分类下已产生商品！");
            } else {
                return to(mallStoreSetmealService.removeById(mallStoreSetmeal.getId()));
            }
        }
    }

    @ApiOperation(value = "获取套餐分类详细信息")
    @GetMapping(value = "/getInfo/{id}")
    HttpResult<MallStoreSetmeal> getInfo(@PathVariable("id") Long id) {
        return HttpResult.ok(mallStoreSetmealService.getById(id));
    }


    @ApiOperation(value = "管理后台查询套餐分类列表")
    @PostMapping("/adminSetmealList")
    HttpResult<List<MallStoreSetmeal>> adminSetmealList(@RequestBody MallStoreSetmeal mallStoreSetmeal) {
        QueryWrapper<MallStoreSetmeal> wrapper = Wrappers.query(mallStoreSetmeal);
        return HttpResult.ok(mallStoreSetmealService.selectList(wrapper));
    }
}
